package cn.tx.demo;

import cn.tx.util.DBUtil;

import java.sql.*;

public class JDBCDemo5 {

    static  final  String username="root";
    static  final  String password="root";
    static  final  String url="jdbc:mysql://127.0.0.1:3306/mydb4";
    static  final  String driver="com.mysql.jdbc.Driver";

    public static void insert(){
        String sql="insert into emp( EMP_ID, ENAME , GENDER ,HIREDATE) values(null,?,?,?);";

        PreparedStatement stmt=null;
        try {
            //驱动加载
            stmt=DBUtil.getPrepareStatement(sql);
            stmt.setString(1,"亮哥");
            stmt.setInt(2,1);
            stmt.setDate(3,new Date(System.currentTimeMillis()));

            //执行SQL
            int i = stmt.executeUpdate();
            System.out.println("插入完毕"+i+"条");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeRes(stmt);
        }


    }
    public static void update(){
        String sql="update emp t set t.ename=?,t.gender=?,t.hiredate=? where t.emp_id=?";

        PreparedStatement stmt=null;
        try {
            //驱动加载
            stmt=DBUtil.getPrepareStatement(sql);
            stmt.setString(1,"龙哥");
            stmt.setInt(2,1);
            stmt.setDate(3,new Date(System.currentTimeMillis()));
            stmt.setInt(4,5);

            stmt.executeUpdate();
            System.out.println("执行完毕");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeRes(stmt);
        }


    }
    public static void delete(){
        String sql="delete from emp where emp_id=?;";


        PreparedStatement stmt=null;
        try {
            //驱动加载
            stmt=DBUtil.getPrepareStatement(sql);
            stmt.setInt(1,1);
            stmt.executeUpdate();
            System.out.println("执行完毕");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeRes(stmt);
        }


    }
    public static void select(){
        String sql="select * from emp ";


        PreparedStatement statement = DBUtil.getPrepareStatement(sql);
        ResultSet rs = null;
        try {
            rs = statement.executeQuery();

            while(rs.next()){
                int empId = rs.getInt(1);
                String ename = rs.getString(2);
                int gender = rs.getInt(3);
                Date date = rs.getDate(4);

                System.out.println("empid:"+empId+"  ename:"+ename+"  gender:"+gender+"  date:"+date);


            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("执行完毕");
             DBUtil.closeRes(rs);





    }


    public static void main(String[] args) {

        select();
    }
}
